/* 
 *  Copyright 2009-2010 Michal Trzcinka
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *  http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software distributed
 *  under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
 *  CONDITIONS OF ANY KIND, either express or implied. See the License for the
 *  specific language governing permissions and limitations under the License.
 */
YAHOO.namespace('TRZCINKA.VCARD');

(function() {

  YAHOO.TRZCINKA.VCARD.DimensionsEditor = function(gridModel) {
    this._gridModel = gridModel;

    this._DIALOG_CONTAINER_ID = Dom.generateId();
    var dialogContainer = document.createElement('div');
    dialogContainer.id = this._DIALOG_CONTAINER_ID;
    var body = document.createElement('div');
    Dom.addClass(body, 'bd');
    dialogContainer.appendChild(body);

    var form = document.createElement('form');

    var rowsLabel = document.createElement('label');
    rowsLabel.innerHTML = I18n.t('editor.dimensions_editor.rows');
    form.appendChild(rowsLabel);

    this._rowsInput = document.createElement('input');
    this._rowsInput .id = dialogContainer.id + '_rows';
    form.appendChild(this._rowsInput);

    var columnsLabel = document.createElement('label');
    columnsLabel.innerHTML = I18n.t('editor.dimensions_editor.columns');
    form.appendChild(columnsLabel);

    this._columnsInput = document.createElement('input');
    this._columnsInput.id = dialogContainer.id + '_rows';
    form.appendChild(this._columnsInput);

    body.appendChild(form);

    document.body.appendChild(dialogContainer);

    this._finishHandler = null;
    this._dlg = this._initDialog();
  };

  YAHOO.TRZCINKA.VCARD.DimensionsEditor.prototype._initDialog = function() {
    var that = this;

var dlg = new YAHOO.widget.Dialog(this._DIALOG_CONTAINER_ID, {
      width:"725px",
      fixedcenter:true,
      modal:true,
      visible:false,
      close: false
    });

    var handleOK = function() {
      var rows = that._rowsInput.value;
      var columns = that._columnsInput.value;
      if (that._gridModel.isNewSizeSafe(rows, columns)) {
        that._gridModel.changeDimensions(rows, columns);
        dlg.hide();
      } else {
        confirm(I18n.t('editor.dimensions_editor.confirm_items_removal'), function() {
          that._gridModel.changeDimensions(rows, columns);
          dlg.hide();
        });
      }
    };

    var buttons = [
    {
      text: I18n.t('editor.dimensions_editor.change'),
      handler: handleOK
    },
    {
      text: I18n.t('editor.dimensions_editor.cancel'),
      handler:function() {
        dlg.hide();
      }
    }
    ];
    dlg.cfg.queueProperty("buttons", buttons);

    dlg.render();
    return dlg;
  };

  YAHOO.TRZCINKA.VCARD.DimensionsEditor.prototype.show = function() {
    this._rowsInput.value = this._gridModel.gridData.rows;
    this._columnsInput.value = this._gridModel.gridData.columns;
    this._dlg.show();
  };

})();
